<?php

namespace Partner\Controller;

use Think\Controller;
use Think\IFilter;
use Think\IReq;
use Think\Page;
use Think\JSON;
use Partner\Model\Goods;
use Think\Upload;

class ToolController extends ComController {

    public function ad_list() {
        $p = IFilter::act(IReq::get('p'), 'int') ? IFilter::act(IReq::get('p'), 'int') : 1;
        $page = '15';
        $order = '`order` ASC';
        $where = array(
            'seller_id' => $this->Seller['seller_id']
        );
        $tb_ad = M('ad');
        $ads = $tb_ad->order($order)->where($where)->page($p . ",$page")->select();

        $count = $tb_ad->where($where)->count();
        $Page = new Page($count, $page);
        $show = $Page->show();
        //print_r($show);die;

        $this->assign('page', $show);
        $this->assign('ads', $ads);
        $this->display();
    }

    /*
     * 添加修改广告
     */
    public function ad_edit() {
        $id = IFilter::act(IReq::get('id'), 'int');
        if ($id) {
            $obj = M('ad');
            $where = 'id = ' . $id;
            $adRow = $obj->where($where)->find();
            $json_adRow = JSON::encode($adRow);
        }
        $json_adRow = $json_adRow ? $json_adRow : 'null';
        $this->assign('json_adRow', $json_adRow);

        $where = array(
            'seller_id' => $this->Seller['seller_id']
        );
        $categories = M('category')->where($where)->select();
        $json_categories = JSON::encode($categories);
        $partnet = Goods::categoryParentStruct($categories);
        $json_partent = JSON::encode($partnet);

        $this->assign('json_categories', $json_categories);
        $this->assign('json_partnet', $json_partent);
        $this->display();
    }

    //[广告] 添加和修改动作
    function ad_edit_act() {
        $id = IFilter::act(IReq::get('id'), 'int');
        $content = IReq::get('content');

        //附件上传
        if (isset($_FILES) && $_FILES) {
            $dir = './Public/ad/';
            $upObj = new Upload(array('rootPath'=> $dir));
            $res = $upObj->uploadOne($_FILES['img']);
            $img = $res['savepath'].$res['savename'];
        }

        $adObj = M('ad');
        $dataArray = array(
            //'content' => IFilter::addSlash($content),
            'content' => IFilter::act(IReq::get('type'))== 1 ? $img : IFilter::act(IReq::get('content')),
            'name' => IFilter::act(IReq::get('name')),
            'type' => IFilter::act(IReq::get('type')),
            //'link' => IFilter::addSlash(IReq::get('link')),
            'link' => IFilter::act(IReq::get('link')),
            'start_time' => IFilter::act(IReq::get('start_time')),
            'end_time' => IFilter::act(IReq::get('end_time')),
            'description' => IFilter::act(IReq::get('description')),
            'order' => IFilter::act(IReq::get('order'), 'int'),
            'goods_cat_id' => IFilter::act(IReq::get('goods_cat_id')),
            'seller_id' => $this->Seller['seller_id']
        );

        //$adObj->setData($dataArray);
        if ($id) {
            $dataArray['id'] = $id;
            $adObj->save($dataArray);
        } else {
            $adObj->add($dataArray);
        }
        $this->redirect("ad_list");
    }
    //[广告] 删除
    function ad_del() {
        $id = IFilter::act(IReq::get('id'), 'int');
        if (!empty($id)) {
            $obj = M('ad');
            if (is_array($id) && isset($id[0]) && $id[0] != '') {
                $id_str = join(',', $id);
                $where[] = 'id in (' . $id_str . ')';
            } else {
                $where = $id;
            }
            $res = $obj->delete($where);
            $this->redirect('ad_list');
        } else {
            $this->error('请选择要删除的广告', U('Tool/ad_list'));
        }
    }
}
